#ifndef __MY_CONFIG__
#define __MY_CONFIG__
#include "util.hpp"
#include <memory>
#include <mutex>
#include "../../log_system/xqlog.hpp"
// 该类用于读取配置文件信息
namespace cloud
{
    // 懒汉模式
    const char *Config_File = "cloud.conf";
    class Config
    {
    private:
        time_t hot_time_; // 热点时间
        int server_port_;
        std::string server_ip;
        std::string download_prefix_; // URL路径前缀
        std::string pack_suffix_;     // 打包文件后缀
        std::string pack_dir_;        // 压缩文件的存储路径
        std::string back_dir_;        // 备份文件的存储路径
        std::string backup_file_;     // 备份信息的配置管理
    private:
        static std::mutex _mutex;
        static Config *_instance;
        Config()
        {
            if (ReadConfig() == false)
            {
                xqlog::GetLogger("asynclogger")->Fatal("ReadConfig start");
                return;
            }
            xqlog::GetLogger("asynclogger")->Info("ReadConfig complicate");
        }

    public:
        // 读取配置文件信息
        bool ReadConfig()
        {
            xqlog::GetLogger("asynclogger")->Info("ReadConfig start");

            cloud::FileUtil fu(Config_File);
            std::string content;
            if (!fu.GetContent(&content))
            {
                xqlog::GetLogger("asynclogger")->Error("ReadConfig error");
            }

            Json::Value root;
            cloud::JsonUtil::UnSerialize(content, &root); // 反序列化，把内容转成jaon value格式

            // 要记得转换的时候用上asint，asstring这种函数，json的数据类型是Value。
            hot_time_ = root["hot_time"].asInt();
            server_port_ = root["server_port"].asInt();
            server_ip = root["server_ip"].asString();
            download_prefix_ = root["download_prefix"].asString();
            pack_suffix_ = root["packfile_suffix"].asString();
            pack_dir_ = root["pack_dir"].asString();
            back_dir_ = root["back_dir"].asString();
            backup_file_ = root["backup_file"].asString();
            return true;
        }
        int GetHotTime()
        {
            return hot_time_;
        }
        int GetServerPort()
        {
            return server_port_;
        }
        std::string GetServerIp()
        {
            return server_ip;
        }
        std::string GetDownloadPrefix()
        {
            return download_prefix_;
        }
        std::string GetPackSuffix()
        {
            return pack_suffix_;
        }
        std::string GetPackDir()
        {
            return pack_dir_;
        }
        std::string GetBackupDir()
        {
            return back_dir_;
        }
        std::string GetBackupFile()
        {
            return backup_file_;
        }

    public:
        // 获取单例类对象
        static Config *GetInstance()
        {
#ifdef DEBUG_LOG
            xqlog::GetLogger("asynclogger")->Debug("GetInstance start");
#endif
            if (_instance == nullptr)
            {
                _mutex.lock();
                if (_instance == nullptr)
                {
                    _instance = new Config();
                }
                _mutex.unlock();
            }
#ifdef DEBUG_LOG
            xqlog::GetLogger("asynclogger")->Debug("GetInstance end");
#endif
            return _instance;
        }
    };
    // 静态成员初始化，先写类型再写类域
    std::mutex Config::_mutex;
    Config *Config::_instance = nullptr;
}
#endif
